import Vue from 'vue'
import Vuex from 'vuex'
import axios from '../axios'


Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        user: window.sessionStorage.getItem('user'),
        token: window.sessionStorage.getItem('token'),
        menus: [],
        selectItem:window.sessionStorage.getItem("selectItem")
    },
    getters: {
        menus: state => {
            return state.menus
        },
        user: state => {
            return state.user
        },
        token: state => {
            return state.token
        }

    },
    mutations: {
        SET_USER: (state, user) => {
            state.user = user
            window.sessionStorage.setItem("user", user)
        },

        SET_TOKEN: (state, token) => {
            state.token = token
            window.sessionStorage.setItem("token", token)
        },
        SET_MENU: (state, menus) => {
            state.menus = menus
        },

        RESET_STATE(state) {
            state.menus = []
            state.user = null
            state.token = ""
        },


    },
    actions: {
        GENERATE_ROUTES({commit}) {
            return new Promise(resolve => {
                axios.get('/sysMenu/menuRoute').then(resp => {
                    if (resp.status) {
                        var menuRoutes = formatRoutes(resp.data[0].children)
                        commit('SET_MENU', menuRoutes)
                        resolve(menuRoutes)

                        //home路由
                        let homeRoute = {
                            path: "/home",
                            component: resolve => require(['../views/Home.vue'], resolve),
                            children: [{
                                path: '/index',
                                name: 'Index',
                                meta: {
                                    title: "首页"
                                },
                                component: resolve => require(['../views/Index.vue'], resolve),
                            }]
                        }
                        let len = fmtRoutes.length;

                        if (len == 0) {
                            commit('SET_MENU', homeRoute)
                            resolve()
                        } else {
                            for (let index = 0; index < len; index++) {
                                homeRoute.children[index + 1] = fmtRoutes[index]
                            }
                            router.addRoutes(constantRoutes)
                            commit('SET_MENU', homeRoute)
                            resolve()
                        }
                    }
                })
            })
        }
    }
})



const formatRoutes = (routes) => {

    let fmtRoutes = []
    routes.forEach(route => {
        if (route.children) {
            route.children = formatRoutes(route.children)
        }

        // var str = route.component
        var str = route.component.length > 0 ? route.component : "/Home"

        // 动态加载路由
        let fmtRoute = {
            path: route.path,
            component: (resolve) => require(['@/views' + str + '.vue'], resolve),
            name: route.name,
            title: route.title,
            icon: route.icon,
            hidden: route.hidden,
            children: route.children
        }

        fmtRoutes.push(fmtRoute)

    })
    return fmtRoutes
}